package com.apes.fn.scm.retailOrder.service.retailProcess;

import com.apes.framework.api.SpringManager;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author wuq
 * @create 2021-05-22 10:56
 */
@Component
public class RetailProcessFactory implements InitializingBean {

    private static List<RetailProcess> retailProcessList = null;

    @Override
    public void afterPropertiesSet() {
        if (retailProcessList == null) {
            retailProcessList = new ArrayList<>();
            Map<String, RetailProcess> beansOfType = SpringManager.getApplicationContext().getBeansOfType(RetailProcess.class);
            beansOfType.forEach((key, value) -> retailProcessList.add(value));
        }
    }

    public static RetailProcess get(String orderType) {
        RetailProcess result = null;
        for (RetailProcess process : retailProcessList) {
            if (process.support(orderType)) {
                result = process;
                break;
            }
        }
        if (Objects.isNull(result)) throw new RuntimeException(orderType + " 没有找到对应的处理器，请检查");
        return result;
    }
}
